﻿<Serializable()> _
Public Class Movie
    Inherits MediaFile

#Region "Members"

    Private m_Events As List(Of EventBase)
    Private m_EndOfMovieResults As List(Of Result)

#End Region

#Region "Constructors"

    Public Sub New(ByVal p_ID As Integer, ByVal p_Url As String, ByVal p_Type As MediaFileType, ByVal p_path As String)

        MyBase.New(p_ID, p_Url, p_Type, p_path)
        m_Events = New List(Of EventBase)
        m_EndOfMovieResults = New List(Of Result)
    End Sub

    Public Sub New()
        MyBase.New()
        m_Events = New List(Of EventBase)
        m_EndOfMovieResults = New List(Of Result)
    End Sub


#End Region

#Region "Properties"

    Public Property EndOfMovieResults() As List(Of Result)
        Get
            Return m_EndOfMovieResults
        End Get
        Set(ByVal value As List(Of Result))
            m_EndOfMovieResults = value
        End Set
    End Property


    Public Property Events() As List(Of EventBase)
        Get
            Return m_Events
        End Get
        Set(ByVal value As List(Of EventBase))
            m_Events = value
        End Set
    End Property



#End Region

#Region "XML Serialization"

#Region "Consts"
    Private Const ID_MOVIE As String = "id"
    Private Const URL_FILE As String = "url"
    Private Const FILE_NAME As String = "fileName"
    Private Const FILE_SUFFIX As String = "fileSuffix"
    Private Const IS_ONLINE As String = "isOnline"

    Private Const END_RESULTS As String = "EndResult"
    Private Const EVENTS_ As String = "Events"

#End Region
    Public Function getXMLString(ByVal p_mediaFiles As List(Of MediaFile)) As XElement
        Dim l_xelement As New XElement("Movie")
        l_xelement.Add(New XAttribute(ID_MOVIE, Me.ID))
        l_xelement.Add(New XAttribute(URL_FILE, Me.Url))
        l_xelement.Add(New XAttribute(FILE_NAME, IO.Path.GetFileNameWithoutExtension(Url)))
        l_xelement.Add(New XAttribute(FILE_SUFFIX, IO.Path.GetExtension(Url).Substring(1)))
        l_xelement.Add(New XAttribute(IS_ONLINE, False))

        Dim l_XEvents As New XElement(EVENTS_)
        l_xelement.Add(l_XEvents)
        For Each l_event In Events
            l_XEvents.Add(l_event.GetXMLString(p_mediaFiles))
        Next

        Dim l_XEndMovieResults As New XElement(END_RESULTS)
        l_xelement.Add(l_XEndMovieResults)
        For Each l_result In EndOfMovieResults
            l_XEndMovieResults.Add(l_result.GetXMLString(p_mediaFiles))
        Next
        Return l_xelement
    End Function

    Public Sub XMLDeserialize(ByVal p_xElement As XElement, ByVal p_mediaFiles As List(Of MediaFile))
        For Each l_elemnt In p_xElement.Elements
            If l_elemnt.Name = EVENTS_ Then
                For Each l_xEvent In l_elemnt.Elements
                    Dim l_event As New EventBase

                    l_event.XMLDeserialize(l_xEvent, p_mediaFiles)
                    Me.Events.Add(l_event)
                Next
            ElseIf l_elemnt.Name = END_RESULTS Then
                For Each l_xEndREsult In l_elemnt.Elements
                    Dim l_result As New Result(0)
                    l_result.XmlDesirialize(l_xEndREsult, p_mediaFiles)
                    Me.EndOfMovieResults.Add(l_result)
                Next
            End If
        Next
    End Sub
#End Region


End Class